package com.huawei.bus.minitest.exe;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.huawei.bus.minitest.constant.ConfigConstant;
import com.huawei.bus.minitest.constant.JSONConstant;
import com.huawei.bus.minitest.db.DatasourcePool;

/**
 * 执行sql节点
 * 默认可以执行指定表，字段，和条件
 * 或者可以指定sql
 * @author yichengfeng
 *
 */
public class SqlExecuteImpl implements IExecute {
	
	private String sql;
	private String db;
	private Connection conn;
	private String tableName;
	private String fields;
	private String queryCondition;
	private boolean isReturnList = false;
	
	public SqlExecuteImpl(JSONObject jsObj) throws JSONException {
		sql = jsObj.getString(JSONConstant.SQL_TEXT);
		db = jsObj.getString(JSONConstant.DB);
		conn = getConnectionByDb(db);
		
		if( conn == null){
			//TODO yichengfeng throw exception
		}
		try {
			isReturnList = Boolean.parseBoolean(jsObj.getString(JSONConstant.IS_RETURN_LIST));
		} catch (Exception e) {
			// TODO yichengfeng log error
		}
	}

	private Connection getConnectionByDb(String db2) {
		if(db2 == null || "".equals(db2)){
			//TODO yichengfeng throw exception
		}
		
		if(ConfigConstant.SYSDB.equals(db2)){
			
			try {
				return DatasourcePool.getSysdbConnection();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		if(db2.startsWith(ConfigConstant.USERDB)){
			
			int idx = 0;
			String idxStr = db2.substring(6,7);
			idx = Integer.parseInt(idxStr);
			try {
				return DatasourcePool.getUserdbConnection(idx);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return null;
	}

	@Override
	public Map execute(Map input) {
		
		if(sql == null || "".equals(sql)){
//			TODO yichengfeng compose new sql from tablename,fields,querycondition
		}
		if(!isReturnList){
			Map ret = querySingleRecord(input);
			input.putAll(ret);
		}else{
			List<Map> ret = queryMutiRecord(input);
			input.put("list", ret);
		}
//		executeQuery(input);
		return input;
	}

	private List<Map> queryMutiRecord(Map input) {
		// TODO Auto-generated method stub
		return null;
	}

	private Map querySingleRecord(Map input) {
		// TODO Auto-generated method stub
		return null;
	}

	private void executeQuery(Map input) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DatasourcePool.getSysdbConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				String n = rs.getString("name");
				input.put("name", n);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
		}
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

}
